package com.projects.android.views;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.projects.android.R;
import com.projects.android.bus.UsersBUS;
import com.projects.android.utils.Utils;

public class ForgotPassword extends Activity implements OnClickListener {
	private Intent intent;
	private Button btnCancel;
	private Button btnOK;

	private EditText txtUserName;
	private EditText txtEmail;

	// Use an existing ListAdapter that will map an array
	// of strings to TextViews
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgotpassword);
		initFrm();
	}

	private void initFrm() {
		btnCancel = (Button) findViewById(R.id.btnResetPasswordCancel);
		btnOK = (Button) findViewById(R.id.btnResetPasswordOK);

		txtUserName = (EditText) findViewById(R.id.txtResetPasswordUserName);
		txtEmail = (EditText) findViewById(R.id.txtResetPasswordEmail);
		
		btnOK.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnResetPasswordCancel:
			intent = Utils.browseIntent(this, "Intro");
			startActivity(intent);
			break;
		case R.id.btnResetPasswordOK:
			if (!ValidateReset()) {
				return;
			}
			final ProgressDialog dialog = new ProgressDialog(this);
			dialog.setTitle("Loading. Please wait...");
			dialog.setIndeterminate(true);

			final Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					runOnUiThread(new Runnable() {
						String result = UsersBUS.resetPassword(txtUserName.getText().toString(), txtEmail.getText().toString());
						private AlertDialog dlg;

						@Override
						public void run() {
							dialog.dismiss();
							Log.i("ResetPassword result", result);
							if(result.equals("")){
								dlg = new AlertDialog.Builder(ForgotPassword.this).setTitle("Register").setMessage("Reset successful, please check your email to retrieve new password")
								.setPositiveButton("OK", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int whichButton) {
										intent = Utils.browseIntent(ForgotPassword.this, "Intro");
										startActivity(intent);
									}
								}).create();
								dlg.show();						
							}else{
								dlg = Utils.showAlertDlg(ForgotPassword.this, "Register", "Cannot reset your password: " + result);
								dlg.show();
							}
						}
					});
				}
			});
			dialog.show();
			thread.start();
			break;
		}
	}
	
	public boolean ValidateReset() {
		String errorTxt = "";
		if (txtUserName.getText().toString().equals("")) {
			errorTxt = "UserName is empty";
		}
		else if (txtEmail.getText().toString().equals("")) {
			errorTxt = "Email is empty";
		}
		if (!errorTxt.equals("")) {
			AlertDialog dlg = Utils.showAlertDlg(this, "Validate ResetPassword", errorTxt);
			dlg.show();
			return false;
		}
		return true;
	}
}
